package com.zhuoyue.biz.core.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.biz.core.api.dao.DeviceDao;
import com.zhuoyue.biz.core.api.dao.TblDeviceActionsMapper;
import com.zhuoyue.biz.core.api.domain.DeviceDO;
import com.zhuoyue.biz.core.api.domain.TblDeviceActions;
import com.zhuoyue.biz.core.api.service.DeviceActionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author wjming
 */
@Service
public class DeviceActionServiceImpl extends ServiceImpl<TblDeviceActionsMapper, TblDeviceActions> implements DeviceActionService {
    @Autowired
    private DeviceDao deviceDao;


    @Override
    public Boolean updateDeviceActionById(String id, String action) {
        if (StringUtils.isNotEmpty(action)) {
            //通过id获取设备类型
            DeviceDO device = deviceDao.selectDeviceById(id);
            String deviceTye = device.getOdIndex() + device.getDeviceType() + device.getProductType();
            if (StringUtils.isNotEmpty(deviceTye)) {
                deviceTye = StringUtils.substringBefore(deviceTye, "_");
            }

            JSONObject jsonObject = JSONObject.parseObject(action);
            switch (deviceTye) {
                case "0FBEA002":
                    String temp = (String) jsonObject.get("temp");
                    if (StringUtils.isNotEmpty(temp)) {
                        deviceDao.updateDeviceActionByDeviceId0Faa0C02(id, "temp", temp);
                    }
                    String humidity = (String) jsonObject.get("humidity");
                    if (StringUtils.isNotEmpty(humidity)) {
                        deviceDao.updateDeviceActionByDeviceId0Faa0C02(id, "humidity", humidity);
                    }
                    String tempAlarmTop = (String) jsonObject.get("tempAlarmTop");
                    if (StringUtils.isNotEmpty(tempAlarmTop)) {
                        deviceDao.updateDeviceActionByDeviceId0Faa0C02(id, "tempAlarmTop", tempAlarmTop);
                    }
                    String tempAlarmGorge = (String) jsonObject.get("tempAlarmGorge");
                    if (StringUtils.isNotEmpty(tempAlarmGorge)) {
                        deviceDao.updateDeviceActionByDeviceId0Faa0C02(id, "tempAlarmGorge", tempAlarmGorge);
                    }
                    String humidityAlarmTop = (String) jsonObject.get("humidityAlarmTop");
                    if (StringUtils.isNotEmpty(humidityAlarmTop)) {
                        deviceDao.updateDeviceActionByDeviceId0Faa0C02(id, "humidityAlarmTop", humidityAlarmTop);
                    }
                    String humidityAlarmGorge = (String) jsonObject.get("humidityAlarmGorge");
                    if (StringUtils.isNotEmpty(humidityAlarmGorge)) {
                        deviceDao.updateDeviceActionByDeviceId0Faa0C02(id, "humidityAlarmGorge", humidityAlarmGorge);
                    }
                    break;
                default:
                    break;
            }
        }
        return true;
    }
}
